﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace SPFeatureGenerator
{
    public partial class ContentTypeForm : Form
    {
        private readonly ContentType contentType;

        public ContentType ContentType
        {
            get { return contentType; }
            set { value = contentType; }
        }

        public ContentTypeForm()
        {
            InitializeComponent();
        }

        public ContentTypeForm(ContentType contentType) : this()
        {
            this.contentType = contentType;

            editName.Text = contentType.Name;
            editDescription.Text = contentType.Description;
            editGroup.Text = contentType.Group;

            cmbType.Items.Add(new KeyValuePair<string, string>("Document", "0x010100"));
            cmbType.Items.Add(new KeyValuePair<string, string>("Announcement", "0x010400"));

            cmbType.SelectedIndex = 0;
            for (int idx = 0 ; idx < cmbType.Items.Count; idx ++)
            {
                KeyValuePair<string, string> item = (KeyValuePair<string, string>) cmbType.Items[idx];
                if (item.Value.Equals(contentType.TypePrefix))
                {
                    cmbType.SelectedIndex = idx;
                    break;
                }
            }
        }

        private void btnSelectTemplate_Click(object sender, EventArgs e)
        {
            DialogResult result = openTemplateFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                contentType.TemplateFullName = openTemplateFileDialog.FileName;
                contentType.TemplateName = openTemplateFileDialog.SafeFileName;
                editTemplate.Text = contentType.TemplateName;
            }
        }

        private void editText_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = !Utils.validateTextBox((TextBox)sender);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            contentType.Name = editName.Text;
            contentType.Description = editDescription.Text;
            contentType.Group = editGroup.Text;
            contentType.TypePrefix = ((KeyValuePair<string, string>)cmbType.SelectedItem).Value;

            Close();
        }
    }
}
